package cn.dinxian.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.dinxian.entity.CommonResult;
import cn.dinxian.api.RepositoryService;
import cn.dinxian.api.RuntimeService;
import cn.dinxian.api.TaskService;
import cn.dinxian.entity.BaseDTO;
import cn.dinxian.entity.Task;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

//import org.activiti.api.process.model.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author dingxian
 * @Title:
 * @param:
 * @return:
 * @Date 2023/2/21 10:28
 * @Description:
 */
@RestController
@RequestMapping("/task")
@Slf4j
public class testControllers {
  
  @Autowired
  RepositoryService repositoryService;
  
  @Autowired
  RuntimeService runtimeService;
  
  @Autowired
  TaskService taskService;

  /**
   * 监听器测试
   */
 @GetMapping("t1")
  public String listenerTest() {
    // 1、发布流程，当流程的key相同时则以版本来控制，每次启动流程实例时默认取最新版本
    //Deployment deployment = repositoryService.createDeployment().name("监听器测试流程").addClasspathResource("processes/listener.bpmn20.xml").deploy();

    // 2、启动一个流程实例
    
    String processInstance = runtimeService.startProcessInstanceByKey("Process_4");
    return processInstance; 
  }

  /**
   * 驱动流程
   */
  @GetMapping("t2")
  public CommonResult<Object> listenerTest2() {
    final String process_4 = taskService.TaskQuery("Process_4");
    final JSONObject jsonObject = JSONObject.parseObject(process_4);
    final JSONArray data = jsonObject.getJSONArray("data");
    final List<Task> tasks = JSON.parseArray(JSONObject.toJSONString(data), Task.class);
    Map<String, Object> map = new HashMap<>();
    map.put("FormProperty_opt",0);
     BaseDTO param = BaseDTO.builder()
        .id(tasks.get(0).getId())
        .map_1(map)
        .build();
    //log.info("[MYLOG]:-----testControllers/listenerTest2==>feign:["+JSONObject.toJSONString(param)+"]");
    taskService.complete(param);
     return new CommonResult<Object>(200,null);
   
  }
  
  

}
  